package action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import dao.BookDao;
import dao.impl.BookDaoImpl;
import data.DataLoad;
import pojo.Book;
import jsm.comsuner.JmsConsumer;

public class ConsumeAction {
	
	public void action(){
		JmsConsumer consumer = new JmsConsumer();
		List<Book> books = new ArrayList<Book>();
		ObjectMapper mapper = new ObjectMapper();
		BookDao bookDao = new BookDaoImpl();
		int count = 0;
		long start = System.currentTimeMillis();
		while (true) {
			String bookString= consumer.consume();
			Book book;
			try {
				book = mapper.readValue(bookString, Book.class);
				books.add(book);
				bookDao.addBook(book);
				if (count==9999) {
					long end = System.currentTimeMillis();
					System.out.println(end-start);
				}
				count++;
			} catch (JsonParseException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	public static void main(String[] args) {
//		new ConsumeAction().action();
		for (int i = 0; i < 20; i++) {
			new Thread(new DataLoad()).start();
		}
	}

}
